#import "ARLTimeUtils.h"

@implementation ARLTimeUtils
/**
 anyDate 转成 本地时区的 NSDate
 */
+(NSDate*)getLocalDateFormatAnyDate:(NSDate*)anyDate{
    NSTimeZone *sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT
    NSTimeZone *desTimeZone = [NSTimeZone localTimeZone];
    //得到源日期与世界标准时间的偏移量
    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:anyDate];
    //目标日期与本地时区的偏移量
    NSInteger destinationGMTOffset = [desTimeZone secondsFromGMTForDate:anyDate];
    //得到时间偏移量的差值
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    //转为现在时间
    NSDate* destinationDateNow = [[NSDate alloc] initWithTimeInterval:interval sinceDate:anyDate];
    return destinationDateNow;
}
/**
 将本地日期字符串转为UTC日期字符串
 eg: 2017-10-25 02:07:39 -> 2017-10-24 18:07:39
 */
+(NSString *)getUTCStrFormateLocalStr:(NSString *)localStr {
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *dateFormatted = [format dateFromString:localStr];
    format.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    NSString *dateString = [format stringFromDate:dateFormatted];
    return dateString;
}
/**
 将UTC日期字符串转为本地时间字符串
 eg: 2017-10-25 02:07:39  -> 2017-10-25 10:07:39
 */
+(NSString *)getLocalDateFormateUTCDate:(NSString *)utcStr {
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    format.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    NSDate *utcDate = [format dateFromString:utcStr];
    format.timeZone = [NSTimeZone localTimeZone];
    NSString *dateString = [format stringFromDate:utcDate];
    return dateString;
}



// 2021-11-16 15:00:24  -> 20211116T070024Z
//相对国区有8小时时差
+ (NSString*)currUTCDate{
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"yyyyMMdd'T'HHmmss'Z'";
    NSDate *dateFormatted =[NSDate date];
    format.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    NSString *dateString = [format stringFromDate:dateFormatted];
    return dateString;
}
//相对国区没有时差
+ (NSString*)currUTCDateCN{
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    format.dateFormat = @"yyyyMMdd'T'HHmmss'Z'";
    NSDate *dateFormatted = [ARLTimeUtils getLocalDateFormatAnyDate:[NSDate date]];
    format.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    NSString *dateString = [format stringFromDate:dateFormatted];
    return dateString;
}

+(long long)currTOMilliSeconds{
    return [self getDateTimeTOMilliSeconds:[NSDate date]];
}

//将NSDate类型的时间转换为时间戳,从1970/1/1开始
+(long long)getDateTimeTOMilliSeconds:(NSDate *)datetime{
    NSTimeInterval interval = [datetime timeIntervalSince1970];
    long long totalMilliseconds = interval*1000 ;
    return totalMilliseconds;
}



+ (NSString *)getCurrentTimeStampInMillionSeconds {
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a=[dat timeIntervalSince1970];
    NSString*timeString = [NSString stringWithFormat:@"%0.f", a*1000];
    return timeString;
}

+ (NSString *)getCurrentTimeStampInSeconds {
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a=[dat timeIntervalSince1970];
    NSString*timeString = [NSString stringWithFormat:@"%0.f", a];
    return timeString;
}

+ (NSNumber *)getTimeZoneOffset {
    NSInteger minutesOffsetGMT = - ([[NSTimeZone defaultTimeZone] secondsFromGMT] / 60);
    return @(minutesOffsetGMT);
}
@end
